package test;

import lib.db_properties_path.*;
import java.sql.*;
import java.util.*;
import lib.dbTool.*;

public class CAREGIVER extends DBconnection2 {
	public CAREGIVER(SetPath set) {
		super(set);
	}

	public CAREGIVER() {
		super(new DefaultSetPath());
	}

	/** Auto Increasment */
	/** Primary Key */
	/** int identity ; java.lang.Integer */
	private int idcaregiver;
	/** nvarchar ; java.lang.String */
	private String namecaregiver;
	/** nvarchar ; java.lang.String */
	private String telcaregiver;
	/** nvarchar ; java.lang.String */
	private String emailcaregiver;
	/** nvarchar ; java.lang.String */
	private String accountcaregiver;
	/** nvarchar ; java.lang.String */
	private String passwordcaregiver;

	public int getIdcaregiver() {
		return idcaregiver;
	}

	public void setIdcaregiver(int idcaregiver) {
		this.idcaregiver = idcaregiver;
	}

	public String getNamecaregiver() {
		return namecaregiver;
	}

	public void setNamecaregiver(String namecaregiver) {
		this.namecaregiver = namecaregiver;
	}

	public String getTelcaregiver() {
		return telcaregiver;
	}

	public void setTelcaregiver(String telcaregiver) {
		this.telcaregiver = telcaregiver;
	}

	public String getEmailcaregiver() {
		return emailcaregiver;
	}

	public void setEmailcaregiver(String emailcaregiver) {
		this.emailcaregiver = emailcaregiver;
	}

	public String getAccountcaregiver() {
		return accountcaregiver;
	}

	public void setAccountcaregiver(String accountcaregiver) {
		this.accountcaregiver = accountcaregiver;
	}

	public String getPasswordcaregiver() {
		return passwordcaregiver;
	}

	public void setPasswordcaregiver(String passwordcaregiver) {
		this.passwordcaregiver = passwordcaregiver;
	}

	public interface fn {
		String TABLE_NAME = "CAREGIVER";
		String PK = "idcaregiver";
		String idcaregiver = "idcaregiver";
		String namecaregiver = "namecaregiver";
		String telcaregiver = "telcaregiver";
		String emailcaregiver = "emailcaregiver";
		String accountcaregiver = "accountcaregiver";
		String passwordcaregiver = "passwordcaregiver";
	}

	public String sql(String SQL) {
		return Utility.getSQL組合字(SQL, CAREGIVER.fn.class);
	}

	public int clear() {
		String SQL = "delete from CAREGIVER";
		Object parameter[] = {};
		return super.excuteUpdate(SQL, parameter);
	}

	public int delete(int idcaregiver) {
		String SQL = "delete from CAREGIVER where idcaregiver=?";
		Object parameter[] = { idcaregiver };
		return super.excuteUpdate(SQL, parameter);
	}

	public int update(String namecaregiver, String telcaregiver,
			String emailcaregiver, String accountcaregiver,
			String passwordcaregiver, int idcaregiver) {
		String SQL = "update CAREGIVER set namecaregiver=?,telcaregiver=?,emailcaregiver=?,accountcaregiver=?,passwordcaregiver=? where idcaregiver=?";
		Object parameter[] = { namecaregiver, telcaregiver, emailcaregiver,
				accountcaregiver, passwordcaregiver, idcaregiver };
		return super.excuteUpdate(SQL, parameter);
	}

	public int insert(String namecaregiver, String telcaregiver,
			String emailcaregiver, String accountcaregiver,
			String passwordcaregiver) {
		String SQL = "insert into CAREGIVER(namecaregiver,telcaregiver,emailcaregiver,accountcaregiver,passwordcaregiver) values(?,?,?,?,?)";
		Object parameter[] = { namecaregiver, telcaregiver, emailcaregiver,
				accountcaregiver, passwordcaregiver };
		return super.excuteUpdate(SQL, parameter);
	}

	public String[][] select() {
		String SQL = "select * from CAREGIVER";
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public CAREGIVER[] select_Object() {
		String SQL = "select * from CAREGIVER";
		Object parameter[] = {};
		String data[][] = super.select(SQL, parameter);
		CAREGIVER obj[] = toObject(data);
		return obj;
	}

	public String[][] select(int idcaregiver) {
		String SQL = "select * from CAREGIVER where idcaregiver=?";
		Object parameter[] = { idcaregiver };
		return super.select(SQL, parameter);
	}

	public CAREGIVER[] select_Object(int idcaregiver) {
		String data[][] = this.select(idcaregiver);
		CAREGIVER obj[] = toObject(data);
		return obj;
	}

	public int selectCount() {
		String SQL = "select count(idcaregiver) from CAREGIVER";
		Object parameter[] = {};
		String d[][] = super.select(SQL, parameter);
		return Integer.parseInt(d[0][0]);
	}

	public String[][] selectPage(int pageSize, int pageIndex, int rowCount) {
		int desc = PaggingSQL.ASC;
		PaggingSQL ps = new lib.dbTool.MsSQL2012();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		String SQL = ps.getSelectPage(pageSize, pageIndex, TABLE_NAME, PK,
				desc, rowCount);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public String[][] selectPageOrderBy(int pageSize, int pageIndex,
			String sortName, int desc, int rowCount) {
		PaggingSQL ps = new lib.dbTool.MsSQL2012();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME,
				PK, sortName, desc, rowCount);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	/**
	 * 關鍵字 where 不用寫
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param sortName
	 * @param desc
	 * @param rowCount
	 * @param whereCondition
	 *            = 【pk>0】, 關鍵字 where 不用寫
	 * @return
	 */
	public String[][] selectPageOrderBy(int pageSize, int pageIndex,
			String sortName, int desc, int rowCount, String whereCondition) {
		PaggingSQL ps = new lib.dbTool.MsSQL2012();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		// 範例,關鍵字 where 不用寫, ex:【pk>0】
		String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME,
				PK, sortName, desc, rowCount, whereCondition);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public CAREGIVER[] selectPage_Object(int pageSize, int pageIndex,
			int rowCount) {
		String data[][] = this.selectPage(pageSize, pageIndex, rowCount);
		CAREGIVER obj[] = toObject(data);
		return obj;
	}

	/**
	 * 分組統計筆數
	 * 
	 * @param fk_List
	 * @return
	 */
	public Map<Integer, Integer> getGroupCount(String fkName,
			LinkedList<Integer> fk_List) {
		String q = "";
		Object parameter[] = new Object[fk_List.size()];
		int i = 0;
		for (Integer no : fk_List) {
			q += "?,";
			parameter[i++] = no;
		}
		if (q.length() > 0) {
			q = q.substring(0, q.length() - 1);
		}
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		String SQL = "";
		SQL += "SELECT " + fkName + ", count(" + fkName + ") ";
		SQL += "FROM " + fn.TABLE_NAME + " where " + fkName + " in (" + q + ")";
		SQL += "group by " + fkName + ";";
		String data[][] = super.select(SQL, parameter);
		for (int j = 0; j < data.length; j++) {
			int key = Integer.parseInt(data[j][0]);
			int count = Integer.parseInt(data[j][1]);
			map.put(key, count);
		}
		return map;
	}

	/**
	 * Where PK in(?,?,?....)
	 * 
	 * @param idList
	 * @return
	 */
	public Map<Integer, CAREGIVER> selectMap(LinkedList<Integer> idList) {
		String q = "";
		Object parameter[] = new Object[idList.size()];
		int i = 0;
		for (Integer no : idList) {
			q += "?,";
			parameter[i++] = no;
		}
		if (q.length() > 0) {
			q = q.substring(0, q.length() - 1);
		}
		HashMap<Integer, CAREGIVER> map = new HashMap<Integer, CAREGIVER>();

		String SQL = "select * from CAREGIVER where idcaregiver in (" + q + ")";
		String data[][] = super.select(SQL, parameter);
		CAREGIVER[] obj = this.toObject(data);
		for (CAREGIVER o : obj) {
			map.put(new Integer(o.idcaregiver), o);
		}
		return map;
	}

	public CAREGIVER[] toObject(String data[][]) {
		CAREGIVER[] obj = new CAREGIVER[data.length];
		for (int i = 0; i < obj.length; i++) {
			obj[i] = new CAREGIVER(super.getSetting());
			obj[i].idcaregiver = Integer.parseInt(data[i][0]);
			obj[i].namecaregiver = data[i][1];
			obj[i].telcaregiver = data[i][2];
			obj[i].emailcaregiver = data[i][3];
			obj[i].accountcaregiver = data[i][4];
			obj[i].passwordcaregiver = data[i][5];
		}
		return obj;
	}
}